package com.cddx.logistics.controller;

import com.cddx.logistics.domain.dto.StockOperationCreateDTO;
import com.cddx.logistics.domain.dto.StockOperationQueryDTO;
import com.cddx.logistics.domain.pojo.StockOperation;
import com.cddx.logistics.domain.vo.*;
import com.cddx.logistics.service.StockOperationService;
import com.cddx.logistics.util.model.R;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 出入库记录管理
 */
@RestController
@RequestMapping("/stockOperation")
public class StockOperationController {
    @Resource
    private StockOperationService stockOperationService;

    /**
     * 分页查询出入库记录
     */
    @GetMapping("/queryPage")
    public R queryPage(StockOperationQueryDTO queryDTO) {
        PageInfo<StockOperationVO> pageInfo = stockOperationService.queryPage(queryDTO);
        return R.ok(pageInfo);
    }

    /**
     * 获取出入库记录详情
     */
    @GetMapping("/getById")
    public R getById(@RequestParam Long stockOperationId) {
        StockOperationVO stockOperation = stockOperationService.getById(stockOperationId);
        return R.ok(stockOperation);
    }

    /**
     * 创建出入库记录
     */
    @PostMapping("/create")
    public R create(@RequestBody StockOperationCreateDTO createDTO) {
        boolean result = stockOperationService.create(createDTO);
        return result ? R.ok() : R.error("创建失败");
    }

    /**
     * 更新出入库记录
     */
    @PostMapping("/update")
    public R update(@RequestBody StockOperation stockOperation) {
        boolean result = stockOperationService.update(stockOperation);
        return result ? R.ok() : R.error("更新失败");
    }

    /**
     * 删除出入库记录
     */
    @PostMapping("/delete")
    public R delete(@RequestParam Long stockOperationId) {
        boolean result = stockOperationService.delete(stockOperationId);
        return result ? R.ok() : R.error("删除失败");
    }

    /**
     * 获取配变电统计数据
     */
    @GetMapping("/powerTransformerStats")
    public R powerTransformerStats() {
        PowerTransformerStatsVO stats = stockOperationService.getPowerTransformerStats();
        return R.ok(stats);
    }

    /**
     * 获取计划完成统计数据
     */
    @PostMapping("/planCompletionStats")
    public R planCompletionStats() {
        PlanCompletionStatsVO stats = stockOperationService.getPlanCompletionStats();
        return R.ok(stats);
    }

    /**
     * 备品统计
     */

    @PostMapping("/sparePartsStats")
    public R sparePartsStats(@RequestBody StockOperationQueryDTO queryDTO) {
        Map<String, Object> result = stockOperationService.getSparePartsStats(queryDTO);
        return R.ok(result);
    }

    /**
     * 获取新品统计数据
     */
    @PostMapping("/newProductStats")
    public R newProductStats() {
        NewProductStatsVO stats = stockOperationService.getNewProductStats();
        return R.ok(stats);
    }

    /**
     * 获取电缆统计数据
     */
    @PostMapping("/cableStats")
    public R cableStats() {
        Map<String, Object> stats = stockOperationService.getCableStatistics();
        return R.ok(stats);
    }


    @PostMapping("/lineStats")
    public R lineStats(@RequestBody StockOperationQueryDTO queryDTO) {
        List<LineStatsVO> stats = stockOperationService.getLineStats(queryDTO);
        return R.ok(stats);
    }


    /**
     * 获取临措统计数据
     */
    @PostMapping("/temporaryMeasureStats")
    public R temporaryMeasureStats(@RequestBody StockOperationQueryDTO queryDTO) {
        Map<String, Object> result = stockOperationService.getTemporaryMeasureStats(queryDTO);
        return R.ok(result);
    }


    /**
     * 获取项目完成率统计
     */
    @PostMapping("/projectCompletionRate")
    public R projectCompletionRate() {
        ProjectCompletionRateVO stats = stockOperationService.getProjectCompletionRate();
        return R.ok(stats);
    }

}